<?php
class UserController extends Zend_Controller_Action {
	
	private $user;
	
	public function init() {
		$this->user = new User();
	}
	
	/**
	 * Take in one location and save into db.
	 */
	public function marklocationAction() {
	
	}
	
	/**
	 * Register action.
	 */
	public function registerAction() {
		$form = new RegisterForm();
		$this->view->form = $form;
		if ($this->getRequest ()->isPost ()) {
            if ($form->isValid ( $_POST )) {
            	$email = trim($form->getValue("email"));
            	$password = trim($form->getValue("password"));
            	$idProvider = $form->getValue("idProvider");
            	$currentTime = $this->_helper->generator->generateCurrentTime();
            	$registrationKey = $this->_helper->generator ( 32, 'alpha' );
            	// assemble user data
            	$data = array(
            	   "openid" => $email,
            	   "password" => md5($password),
            	   "created" => $currentTime,
            	   "status" => UserStatus::PENDING,
            	   "registration_key" => $registrationKey
            	);
            	$db = Zend_Registry::get("db");
            	$db->beginTransaction();
            	$user = $this->user->addUser($idProvider, $data);
            	$this->_helper->swiftEmail($email, $registrationKey);
            	$db->commit();
            	$this->_redirect("/user/login");
            }
		}
	}
	
    /**
     * User activate action.
     */
    public function activateAction() {
    	$key = $this->_getParam("key");
    	$email = $this->_getParam("email");
    	$user = $this->user->findUserByKeyAndEmail($key, $email);
    	if ($user->status == UserStatus::PENDING) {
    		$this->user->updateUserStatus($user->id, UserStatus::ACTIVE);
    		$this->_redirect("/user/login");
    	} else {
    		echo "User not existed or can not be activate! It might because user has already be activated.";
    	}
    }
	
	
	/**
	 * Login action.
	 */
	public function loginAction() {
		$form = new LoginForm();
		$this->view->form = $form;
		
		if ($this->getRequest ()->isPost ()) {
            if ($form->isValid ( $_POST )) {
//            	$email = trim($form->getValue("email"));
//            	$password = trim($form->getValue("password"));
            	
                $user = new User ();
                $data = $form->getValues ();
                $auth = Zend_Auth::getInstance ();
                $authAdapter = new Zend_Auth_Adapter_DbTable ( $user->getAdapter (), 'user', 'openid', 'password', 'md5(?) AND open_id_provider = "trassol" AND status = "active"' );
                $authAdapter->setIdentity ( $data ['email'] )->setCredential ( $data ['password'] );
                $result = $auth->authenticate ( $authAdapter );
                
                if ($result->isValid ()) {
                    $storage = new Zend_Auth_Storage_Session ( );
                    $storage->write ( $authAdapter->getResultRowObject () );
                    $this->_redirect ( '/' );
                } else {
                    $this->_redirect("/user/login");
                }
            }
		}
	}
	
	/**
	 * Logout action.
	 */
	public function logoutAction() {
		$storage = new Zend_Auth_Storage_Session ( );
        $storage->clear ();
        $this->_redirect ( '/user/login' );
	}
}
?>